package com.news.control;

import com.news.data.entity.User;
import com.news.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Author : xuzhiyong
 * Date : 2022/5/23 9:29
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index() {
        return "user/userLogin";
    }

    @RequestMapping("/userList")
    public String userList(Model model) {
        System.out.println("查询所有user");
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "user/userList";
    }

    @RequestMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
        if (userService.userLogin(username,password)!= null)
            return "redirect:/userList";
        else
            return "error";
    }

}
